//
//  YLEditAdressViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/8.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLEditAdressViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import "YLMyDefaultAddressViewModel.h"
#import "YLMyAdressModel.h"
#import "YLMyDefaultAddressViewModel.h"
#import "NSString+Regex.h"

@interface YLEditAdressViewModel()

@property (nonatomic, strong) YLMyDefaultAddressViewModel *defaultModel;

@end

@implementation YLEditAdressModel
@end

@implementation YLEditAdressViewModel

- (instancetype)init
{
    if (self = [super init]) {
        _defaultModel = [[YLMyDefaultAddressViewModel alloc] init];
        
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    _finshEnableSingal = [RACSignal combineLatest:@[RACObserve(self, nameText), RACObserve(self, phoneText), RACObserve(self, adressText), RACObserve(self, detailAdressText)] reduce:^id _Nullable(NSString *nameText, NSString *phoneText, NSString *adressText, NSString *detailAdressText){
        return @(![adressText isEqualToString:@"请选择地址"] && phoneText.length && nameText.length && detailAdressText.length);
    }];
    
}

- (RACCommand *)addAdressCommand
{
    if (!_addAdressCommand) {
        __weak typeof(self) weakSelf = self;
        _addAdressCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                if (![weakSelf.phoneText isValidPhoneNumber]) {
                    [WHProgressHUD showError:@"请输入正确的手机号"];
                    [subscriber sendCompleted];
                    return nil;
                }
                
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:7];
                
                params[@"Province"] = weakSelf.province;// 省
                params[@"City"] = weakSelf.city;// 市
                params[@"Area"] = weakSelf.area;// 区
                params[@"ReceiveAddress"] = weakSelf.detailAdressText;// 具体地址
                params[@"ReceiveName"] = weakSelf.nameText;// 收货人
                params[@"ReceiveTel"] = weakSelf.phoneText;// 收货手机号
                params[@"IsDefault"] = @(YES);// 是否默认
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/AddReceiveAddress", YLURL];
                [WHProgressHUD showStatus:@"正在创建地址,请勿退出"];
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLMyAdressModel *model = [[YLMyAdressModel alloc] init];
                        model.province = weakSelf.province;
                        model.city = weakSelf.city;
                        model.area = weakSelf.area;
                        model.receiveAddress = weakSelf.detailAdressText;
                        model.receiveName = weakSelf.nameText;
                        model.receiveTel = weakSelf.phoneText;
                        model.receiveAddressId = [YLEditAdressModel yy_modelWithDictionary:responseObject[@"httpResult"]].addressId;
       
                        [WHProgressHUD showSuccess:@"地址添加成功"];
                        
                        [[weakSelf.defaultModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
                            [subscriber sendNext:model];
                            [subscriber sendCompleted];
                        }];
                        
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [subscriber sendCompleted];
                    }
                    
                    
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    return _addAdressCommand;
}

- (void)setAdressModel:(YLMyAdressModel *)adressModel
{
    _adressModel = adressModel;
    _province = adressModel.province;
    _city = adressModel.city;
    _area = adressModel.area;
    _detailAdressText = adressModel.receiveAddress;
    _nameText = adressModel.receiveName;
    _phoneText = adressModel.receiveTel;

}

- (RACCommand *)editAdressCommand
{
    
    if (!_editAdressCommand) {
        __weak typeof(self) weakSelf = self;
        _editAdressCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
               
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:8];
                
                params[@"Province"] = weakSelf.province;// 省
                params[@"City"] = weakSelf.city;// 市
                params[@"Area"] = weakSelf.area;// 区
                params[@"ReceiveAddress"] = weakSelf.detailAdressText;// 具体地址
                params[@"ReceiveName"] = weakSelf.nameText;// 收货人
                params[@"ReceiveTel"] = weakSelf.phoneText;// 收货手机号
                params[@"IsDefault"] = @(YES);// 是否默认
                params[@"ReceiveAddressId"] = @([weakSelf.adressModel.receiveAddressId integerValue]);
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/ModifyReceiveAddress", YLURL];
                [WHProgressHUD showStatus:@"正在修改地址,请勿退出"];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        [WHProgressHUD showSuccess:@"地址修改成功"];
                        weakSelf.adressModel.province = weakSelf.province;
                        weakSelf.adressModel.city = weakSelf.city;
                        weakSelf.adressModel.area = weakSelf.area;
                        weakSelf.adressModel.receiveAddress = weakSelf.detailAdressText;
                        weakSelf.adressModel.receiveName = weakSelf.nameText;
                        weakSelf.adressModel.receiveTel = weakSelf.phoneText;
                        
                        [[weakSelf.defaultModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
                            [subscriber sendNext:nil];
                            [subscriber sendCompleted];
                        }];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [subscriber sendCompleted];
                    }
                    
                    
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                
                return nil;
            }];
        }];
    }
    return _editAdressCommand;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [[[YLMyDefaultAddressViewModel alloc] init].requestCommand execute:nil];
    [WHProgressHUD dismiss];
}

@end
